<template>
	<view class="loginpage">
		<image src="https://changban.dianjingkeji.net/img/素材.png" mode=""></image>
		<view class="title">
			<view class="top">
				<view class="content">
					一生长伴·慢病
				</view>
				<view class="blue">
					无忧
				</view>
			</view>
			<view class="txt">
				为您的家庭保驾护航
			</view>
		</view>
		 <button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权登录</button>
		<!--  <button class="btn" @click="shouquandenglu">授权登录</button> -->
		 <view class="xieyiline">
		 	<view class="yuan " v-if="!read" @click="read=true">
		 		
		 	</view>
			<view class="yuan1" v-if="read" @click="read=false">
				<view class="quan">
					
				</view>
			</view>
			我已阅读并同意
			<view class="xieyi" @click="goxieyi">
				《用户使用协议》
			</view>
			和
			<view class="xieyi"  @click="goyinsi">
				《隐私协议》
			</view>
		 </view>
	</view>
</template>

<script>
	import {login} from '../../utils/api.js'
	export default {
		data() {
			return {
				code:'',
				read:false
			}
		},
		onLoad(e) {
			if(e.phone){
				uni.setStorageSync('tuiphone')
				
			}
		},
		onShow() {
			 this.getWxLoginCode(); // 页面加载时先拿微信登录 code
		},
		methods: {
			goyinsi(){
				uni.navigateTo({
					url:'/pagesA/yinsi/yinsi'
				})
			},
			goxieyi(){
				uni.navigateTo({
				url:'/pagesA/xieyi/xieyi'
				})
			},
	
			
			// async getPhoneNumber(e) {
			
			//   if (!this.read) {
			//     return uni.showToast({
			//       title: '请阅读协议',
			//       icon: 'none'
			//     })
			//   }
			
			//   // 先获取微信登录 code
			//   const loginRes = await wx.login();
			
			//   const params = {
			//     wxcode: loginRes.code, // ❗登录凭证
			//     code: e.detail.code // ❗手机号凭证
			//   };
			
			//   const res = await login(params);
			//    console.log(res)
			//   if (res.data.code == 200) {
			//     uni.setStorageSync('phone', res.data.phone);
			//     uni.setStorageSync('token', res.data.token);
			//     uni.navigateBack();
			//   } else {
			//     uni.showToast({ title: res.data.msg, icon: 'none' })
			//   }
			// },
			 async getWxLoginCode() {
			    const loginRes = await wx.login();
			    this.wxcode = loginRes.code; // 缓存下来
			  },
			  async getPhoneNumber(e) {
			    if (!this.read) {
			      return uni.showToast({
			        title: '请阅读协议',
			        icon: 'none'
			      })
			    }
			
			    if (!e.detail.code) {
			      return uni.showToast({
			        title: '未获取到授权信息',
			        icon: 'none'
			      })
			    }
			
			    const params = {
			      wxcode: this.wxcode,   // ✅ 预先准备好的登录 code
			      code: e.detail.code    // ✅ 手机号凭证
			    };
			
			    const res = await login(params);
			    console.log(res);
			
			    if (res.data.code === 200) {
					uni.setStorageSync('phone', res.data.phone);
					uni.setStorageSync('token', res.data.token);
					if(res.data.user_type==0){
						if(uni.getStorageSync('tuiphone')){
							let a={
								phone:res.data.phone,
								doctorPhone:uni.getStorageSync('tuiphone')
							}
							buyService(a).then(res=>{
								console.log(res)
								uni.clearStorageSync('tuiphone')
							})
						}
						
						 uni.navigateBack();
					}
					if(res.data.user_type==1||res.data.user_type==2){
						uni.setStorageSync('type',res.data.user_type)
						uni.redirectTo({
							url:'/pages/newxueshu/newxueshu'
						})
					}
			     
			     
			    } else {
			      uni.showToast({ title: res.data.msg, icon: 'none' });
			      // code失效则重新登录
			      this.getWxLoginCode(); 
			    }
				
			
			 
			
			
			 // getPhoneNumber (e) {
				//  let that=this
				//  if(that.read){
				// 	wx.login({
				// 	  success (res) {
				// 	    if (res.code) {
				// 	    console.log(res)
						
				// 						let p={
				// 							wxcode:res.code,
				// 							code:e.detail.code,
				// 							encryptedData:e.detail.encryptedData,
				// 							iv:e.detail.iv
				// 						}
				// 						login(p).then(res=>{
				// 							console.log(res,'登录')
				// 							if(res.data.code==200){
				// 								uni.setStorageSync('phone',res.data.phone)
				// 								uni.setStorageSync('token',res.data.token)
				// 								uni.redirectTo({
				// 									url:'/pages/index/index'
				// 								})
				// 							}
				// 						})
				// 	    } else {
				// 	      console.log('登录失败！' + res.errMsg)
				// 	    }
				// 	  }
				// 	}) 
				//  }else{
				// 	 uni.showToast({
				// 	 	title: '请阅读协议',
				// 		icon:'none',
				// 	 	duration: 2000
				// 	 });
				//  }
				
			   
				
			   
			   }
		}
	}
</script>

<style lang="less" scoped>
.loginpage{
	width: 750rpx;
	image{
		width: 600rpx;
		height: 600rpx;
		margin: 185rpx 125rpx 50rpx 125rpx;
		
		
	}
	.title{
	width: 90%;
	text-align: center;
	
	margin: auto;
	.top{
		display: flex;
		
		width: 100%;
		align-items: center;
		font-size: 50rpx;
		justify-content: center;
		.blue{
			color: #0E6BD3 ;
		}
	}
	.txt{
		text-align: center;
		margin-top: 30rpx;
	}
	}
	.btn{
		width: 90%;
		background-color: #1a4f8a;
		color: white;
		height: 80rpx;
		line-height: 80rpx;
		
		margin: auto;
		margin-top: 50rpx;
		border-radius: 40rpx;
		text-align: center;
		font-size: 30rpx;
		font-weight: bold;
	}
	.xieyiline{
		width: 90%;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		margin-top: 60rpx;
		.yuan{
			width: 46rpx;
			height: 46rpx;
			border-radius: 50%;
			box-sizing: border-box;
			border: 1rpx solid gray;
			margin-right: 20rpx;
		}
		.yuan1{
			width: 46rpx;
			height: 46rpx;
			border-radius: 50%;
			box-sizing: border-box;
			border: 1rpx solid #0E6BD3;
			margin-right: 20rpx;
		    line-height: 46rpx;
			padding: 5rpx;
			.quan{
				width: 34rpx;
			height: 34rpx;
			margin: auto;
			
			border-radius: 50%;
			background-color: #0E6BD3;
			}
		}
		.xieyi{
			color: #0E6BD3;
			
		}
	}
	
}
</style>
